{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 购物车程序项目\n",
    "要求：  \n",
    "1、运行程序后，让用户输入支付宝余额，然后打印商品列表给用户。  \n",
    "2、让用户输入商品编号进行购买。  \n",
    "3、用户选择商品后，检查用户的余额是否充足，若够则直接扣款，不够则提醒用户。  \n",
    "4、用户可以随时推出购买，退出时打印用户已购买的商品和支付宝余额。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的余额：50\n",
      "------- 请选择商品编号 欢迎选购 -------\n",
      "商品编号: 0,商品名称: 珍珠奶茶,商品价格: 7\n",
      "商品编号: 1,商品名称: 大杯珍珠奶茶,商品价格: 8.5\n",
      "商品编号: 2,商品名称: 加大杯珍珠奶茶,商品价格: 9.5\n",
      "商品编号: 3,商品名称: 桶装珍珠奶茶,商品价格: 20\n",
      "请正确输入购买商品的编号：1\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "'str' object is not callable",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-2-b6b368cf11d8>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     34\u001b[0m                 \u001b[0muser_shopping_car\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mgoods_list\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mchoice\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     35\u001b[0m                 \u001b[0mAlipay\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mAlipay\u001b[0m \u001b[1;33m-\u001b[0m \u001b[0mgoods_list\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mchoice\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 36\u001b[1;33m                 \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"%s已经加入您的购物车，您的账号余额欢迎: %s元\"\u001b[0m\u001b[1;33m%\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mgoods_list\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mchoice\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mAlipay\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     37\u001b[0m             \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     38\u001b[0m                 \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"您的余额不足，请更换支付方式\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: 'str' object is not callable"
     ]
    }
   ],
   "source": [
    "# 商品的数据信息\n",
    "goods_list = [\n",
    "    # [商品名称。商品价格]\n",
    "    ['珍珠奶茶',7],\n",
    "    ['大杯珍珠奶茶',8.5],\n",
    "    ['加大杯珍珠奶茶',9.5],\n",
    "    ['桶装珍珠奶茶',20]\n",
    "]\n",
    "user_shopping_car = [\n",
    "    \n",
    "]\n",
    "# 用户输入 使用input, input的使用，结果是一个字符串\n",
    "Alipay = int(input(\"请输入您的余额：\"))\n",
    "\n",
    "while True:\n",
    "    # 循环遍历商品列表，将商品信息按照一定格式输出\n",
    "    # 一定格式：商品编号：xx，商品名称：xx，商品价格：xx\n",
    "    print(\"------- 请选择商品编号 欢迎选购 -------\")\n",
    "    for i in range(len(goods_list)):# range会根据商品列表的长度变化而变化\n",
    "        print(\"商品编号: %s,商品名称: %s,商品价格: %s\"%(i,goods_list[i][0],goods_list[i][1]))\n",
    "\n",
    "    # 让用户输入商品编号进行商品的购买\n",
    "    # 用户输入的内容是否符合规范（用户购买的东西有没有）\n",
    "\n",
    "    choice = input(\"请正确输入购买商品的编号：\")\n",
    "    # 1.如果没有 返回\n",
    "    if choice.isdigit():# isdigit判断字符串里面的内容是否数值\n",
    "        choice = int(choice)\n",
    "        if choice > len(goods_list):\n",
    "            print(\"您选择的商品不存在\")\n",
    "    # 2.如果有 判断金额是否够:用户金额Alipay ，商品的金额goods_list[choice][1]\n",
    "        else:\n",
    "            if Alipay > goods_list[choice][1]:\n",
    "                user_shopping_car.append(goods_list[choice])\n",
    "                Alipay = Alipay - goods_list[choice][1]\n",
    "                print(\"%s已经加入您的购物车，您的账号余额: %s元\"%(goods_list[choice][0])(Alipay))\n",
    "            else:\n",
    "                print(\"您的余额不足，请更换支付方式\")\n",
    "    elif choice == \"q\":\n",
    "        # 用户退出购买\n",
    "        print(\"用户购物车：\",user_shopping_car)\n",
    "        print(\"您的余额：\",Alipay)\n",
    "    else:\n",
    "        print(\"请输入商品编号！\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
